function To_Create_Room() {
    wx.navigateTo({
      url: '/pages/createroom/createroom',
    })
}

// // 获取歌单
// function getPlaylistFromStorage() {

//   let AllsongsFromStorage = wx.getStorageSync('songs') || [];
//   let PlayList = AllsongsFromStorage.filter(item => item.isAdded === true);
//   wx.setStorageSync('PlayList', PlayList);
//   return PlayList;

// }

// 添加歌曲到歌单
function addToPlaylist(songMid) {
  console.log(songMid);
  let AllsongsFromStorage = wx.getStorageSync('songs') || [];
  let selectedInAll = AllsongsFromStorage.find(item => item.songmid === songMid);

  let PlayList = wx.getStorageSync('MusicFromData') || [];

  if(PlayList.length === 0) {
    PlayList.push(selectedInAll);
    wx.setStorageSync('MusicFromData', PlayList);
  } else {
    if(PlayList.filter(item => item.songmid === songMid).length === 0) {
      PlayList.push(selectedInAll);
      wx.setStorageSync('MusicFromData', PlayList)
    } else {
      console.log("该歌曲已添加过")
    }
  }

}

// // 从歌单中删除歌曲
// function removeFromPlaylist(songMid) {

//   let AllsongsFromStorage = wx.getStorageSync('songs') || [];
//   let selectedInAll = AllsongsFromStorage.find(item => item.songmid === songMid);
  
//   if(!selectedInAll.isAdded) {
//     wx.showToast({
//       title: '删除成功',
//       icon: 'success',
//       duration: 1000
//     });

//     let PlayList = wx.getStorageSync('MusicFromData');
//     PlayList.filter(item => item.songmid !== songMid);
//     wx.setStorageSync('MusicFromData', PlayList);
//   }

// }

// // 获取收藏夹
// function getLovelistFromStorage() {

//   let getAllsongsFromStorage = wx.getStorageSync('songs') || [];
//   return getAllsongsFromStorage.filter(item => item.isFavorite === true)

// }

// 添加歌曲到收藏
function LovelistChange(songMid) {

  let AllsongsFromStorage = wx.getStorageSync('HistoryList') || [];
  let LoveSong = wx.getStorageSync('LoveList') || [];
  let selectedInAll = AllsongsFromStorage.find(item => item.songmid === songMid);
  console.log(selectedInAll);
  if(LoveSong.length === 0) {
    LoveSong.push(selectedInAll);
    wx.setStorageSync('LoveList', LoveSong);
    wx.showToast({
      title: '已添加到收藏夹',
      icon: 'success',
      duration: 1000
    });
  }
  else {
    if(LoveSong.filter(item => item.songmid === songMid).length === 0) {
      LoveSong.push(selectedInAll);
      wx.setStorageSync('LoveList', LoveSong);
      wx.showToast({
        title: '已添加到收藏夹',
        icon: 'success',
        duration: 1000
      });
    } else {
      LoveSong.filter(item => item.songmid !== songMid);
      wx.setStorageSync('LoveList', LoveSong);
      wx.showToast({
        title: '已取消收藏',
        icon: '',
        duration: 1000
      });
    }
  }

}


// // 获取播放历史
// function getHistorylistFromStorage() {

//   let getAllsongsFromStorage = wx.getStorageSync('songs') || [];
//   return getAllsongsFromStorage.filter(item => item.isListened === true)

// }

function userlogin() {
  wx.login({
      success: res => {
      wx.request({
          //传送code值
          url: 'http://localhost:3000/login?code =' + res.code,
          data: {
              code: res.code
          },
          success: res => {
              console.log("后端code登录请求", res.data)
              wx.setStorageSync('usertoken', res.data.data.token);
              wx.setStorageSync("uid", res.data.data.ures[0].Id);
          }
      })
  }
  })
};


module.exports = {
  To_Create_Room: To_Create_Room,
  // getPlaylistFromStorage: getPlaylistFromStorage,
  addToPlaylist: addToPlaylist,
  // removeFromPlaylist: removeFromPlaylist,
  userlogin : userlogin,
  // getLovelistFromStorage: getLovelistFromStorage,
  LovelistChange: LovelistChange,
  // getHistorylistFromStorage: getHistorylistFromStorage,
  userlogin: userlogin,
};



